package com.limao.servlet;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.patchca.color.ColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

@WebServlet(name="Patchca", urlPatterns="/Patchca",asyncSupported=true)
public class PatchcaServlet  extends HttpServlet {


	private static final long serialVersionUID = -2196840534385048745L;

	private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
	private static Random random = new Random();
   
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		try{
			cs.setColorFactory(new ColorFactory() {
				@Override
				public java.awt.Color getColor(int x) {
					int[] c = new int[3];
					int i = random.nextInt(c.length);
					for (int fi = 0; fi < c.length; fi++) {
						if (fi == i) {
							c[fi] = random.nextInt(71);
						} else {
							c[fi] = random.nextInt(256);
						}
					}
					return new java.awt.Color(c[0], c[1], c[2]);
				}
			});
			RandomWordFactory wf = new RandomWordFactory();
			wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
			wf.setMaxLength(4);
			wf.setMinLength(4);
			cs.setWordFactory(wf);

			switch (random.nextInt(5)) {
			case 0:
				cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
				break;
			case 1:
				cs.setFilterFactory(new MarbleRippleFilterFactory());
				break;
			case 2:
				cs.setFilterFactory(new DoubleRippleFilterFactory());
				break;
			case 3:
				cs.setFilterFactory(new WobbleRippleFilterFactory());
				break;
			case 4:
				cs.setFilterFactory(new DiffuseRippleFilterFactory());
				break;
			}
			setResponseHeaders(response);
			HttpSession session =request.getSession();
			String code = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
			session.setAttribute("code", code);
		} catch (Exception e){
			throw new RuntimeException(e);
		}
	}

    protected void setResponseHeaders(HttpServletResponse response) {
        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);
    }
	
	
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}


	public void init() throws ServletException {
		
	}

	public void destroy() {
		super.destroy(); 
	
	}
}
